# -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name:        OutService.py
# Purpose:
#
# Author:      Drago@
#
# Created:     19.08.2009
# Copyright:   (c) Drago@ 2009
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import Pyro.core

from pygs.interfaces import IOutServiceInterface, IPersistenceInterface, BaseService
from pygs.logger import logger

from pygs.decorators import hasRight

KMLFileTemplate = r"""
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
  <Document>
    <name>%s</name>
    <description>%s</description>

    <Style id="greenLine">
      <LineStyle>
        <color>ff00ff00</color>
        <width>3</width>
      </LineStyle>
    </Style>

    <Placemark>
      <name>Green Line 1</name>
      <styleUrl>#greenLine</styleUrl>
      <LineString>
        <altitudeMode>relative</altitudeMode>
        <coordinates>
%s
        </coordinates>
      </LineString>
    </Placemark>

  </Document>
</kml>"""

class OutService(BaseService, Pyro.core.ObjBase, IOutServiceInterface):
    """
        "In" service implementation
    """

    def __init__(self):
        Pyro.core.ObjBase.__init__(self)

    def startup(self, params = None):
        BaseService.startup(self, params)
        pass

    def shutdown(self):
        BaseService.shutdown(self)
        pass

    @hasRight('GetTrek')
    def getTrek(self, token, trekingObjectId, dateFrom, dateTo):
        # TOOD: input parameters check

        # get trek
        pointsInfo = self.getServiceProvider().implementation(IPersistenceInterface).getPoints(token, trekingObjectId, dateFrom, dateTo)
        pointCoords = ''
        for point in pointsInfo:
            pointCoords += "%s,%s,%s\n" % (point.longitude, point.latitude, point.altitude)
        trek = KMLFileTemplate % ('Trek Name', 'Trek Description', pointCoords)
        
        logger.log("Got trek for object '%s' on period from '%s' to '%s' by token '%s':\n%s" % 
                   (trekingObjectId, dateFrom, dateTo, token, trek))    
        
        return trek


